#
# Classifier for Postfix/Vmailer bounce messages
#
package Mail::Bounce::PHQuery;

use strict;

use vars qw(@ISA @patterns);

@ISA = qw(Mail::Bounce::Heuristic);


$patterns[0] = << 'EoF';
 --------Message not delivered to the following:

${-\s+}${RECIP-\S+}    No matches to nameserver query

 --------Error Detail (phquery V4.1):

 The message, "No matches to nameserver query," is generated whenever
 the ph nameserver fails to locate either a ph alias or name field that
 matches the supplied name.  The usual causes are typographical errors or
 the use of nicknames.  Recommended action is to use the ph program to
 determine the correct ph alias for the individuals addressed.  If ph is
 not available, try sending to the most explicit form of the name, e.g.,
 if mike-fox fails, try michael-j-fox.
EoF

$patterns[1] = << 'EoF';
 --------Message not delivered to the following:

${-\s+}${RECIP-\S+}    No matches to nameserver query

 --------Error Detail (phquery V4.2):

${JUNK-(?s:.*)?}

 --------Unsent Message below:
EoF

$patterns[2] = << 'EoF';
 --------Message not delivered to the following:

${-\s+}${RECIP-\S+}    No matches to nameserver query

 --------Error Detail (phquery V4.4):

 The message, "No matches to nameserver query," is generated whenever
 the ph nameserver fails to locate either a ph alias or name field that
 matches the supplied name.  The usual causes are typographical errors or
 the use of nicknames.  Recommended action is to use the ph program to
 determine the correct ph alias for the individuals addressed.  If ph is
 not available, try sending to the most explicit form of the name, e.g.,
 if mike-fox fails, try michael-fox or michael-j-fox.
EoF

$patterns[3] = <<'EoF';
 --------Message not delivered to the following:

${-\s+}${RECIP-\S+}    ${DIAG-.*}

 --------Error Detail (phquery V3.13):

${JUNK-(?s:.*)}


 --------Unsent Message below:
EoF

sub validate {
    my $self = shift;
    my $keywords;

    $keywords = $self->patternmatch($self->bodyhandle->as_string, @patterns);
    return defined($keywords);
}

sub recipients {
    my $self = shift;
    my $keywords;

    $keywords = $self->patternmatch($self->bodyhandle->as_string, @patterns);
    my $postmaster = $self->head->get("From");
    chop $postmaster;
    my $domain = $postmaster;
    $domain =~ s/^.*@//;
    if (defined($keywords)) {
	if (wantarray()) {
	    my $res = new Mail::Bounce::Address;
	    $res->type("permfail");
	    $res->action("failed");
	    $res->final_recipient($$keywords{RECIP} . "\@" . $domain);
	    $res->diagnostic($$keywords{DIAG})
		if $$keywords{DIAG};
	    return $res;
	} else {
	    return 1;
	}
    } else {
	die "PHQuery: Unmatched pattern in recipients()\n";
    }
}
1;




